package cn.wolfcode.edu.service.impl;

import cn.wolfcode.edu.domain.*;
import cn.wolfcode.edu.mapper.ClientMapper;
import cn.wolfcode.edu.mapper.FormalStudentMapper;
import cn.wolfcode.edu.mapper.TuitionIncomeMapper;
import cn.wolfcode.edu.query.QueryObject;
import cn.wolfcode.edu.service.IFormalStudentService;
import cn.wolfcode.edu.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by 徐沅澧 on 2018/7/9.
 */
@Service
public class FormalStudentServiceImpl implements IFormalStudentService {

    @Autowired
    private FormalStudentMapper studentMapper;

    @Autowired
    private ClientMapper clientMapper;

    @Autowired
    TuitionIncomeMapper incomeMapper;

    public PageResult query(QueryObject qo) {
        int total = studentMapper.queryForCount(qo);
        if (total == 0) {
            return new PageResult();
        }
        List<FormalStudent> rows = studentMapper.queryForList(qo);
        return new PageResult(total,rows);
    }

    public void update(FormalStudent student) {
        //更新关联的client对象
        clientMapper.updateForStudentEdit(student.getClient());
        //更新其他字段
        studentMapper.updateForEdit(student);
    }

    @Override
    public void updateForTransfer(FormalStudent student) {
        studentMapper.updateForTransfer(student);
    }

    @Override
    public void quitStudent(Long id, int statusVal) {
        studentMapper.quitStudent(id,statusVal);
    }

    @Override
    public List<FormalStudent> listAllNotYetPay() {
        return studentMapper.listAllNotYetPay();
    }

    @Override
    public void client2Formal(Client client, FormalStudent formalStudent) {
        SystemDictionaryItem item = new SystemDictionaryItem();
        item.setId(60L);
        //设置client的状态为正式学员
        client.setStatue(item);
        //更新client
        clientMapper.updateStatus2Formal(client);
        //缴费信息
        TuitionIncome income = new TuitionIncome();
        income.setStudent(formalStudent);
        income.setClz(formalStudent.getClz());
        income.setAmountMoney(formalStudent.getAdvance().add(formalStudent.getAlreadyPaid()));
        income.setAlreadyPaid(BigDecimal.ZERO);
        income.setAlreadyPaid(formalStudent.getTotalTuition());
        income.setPaymentMethod(formalStudent.getPaymentMethod());
        income.setReceipt(UUID.randomUUID().toString());
        income.setCashier((Employee) SecurityUtils.getSubject().getPrincipal());
        income.setPaymentTime(formalStudent.getPaymentTime() == null ? new Date() : formalStudent.getPaymentTime());

        //拿到封装的对象封装学生数据
        formalStudent.setClient(client);
        formalStudent.setStudentName(client.getName());
        formalStudent.setTel(client.getTel());
        formalStudent.getSaleman().setId(client.getSaleman().getId());
        formalStudent.setSchool(client.getSchool());
        formalStudent.setClz(client.getClz());
        //学费信息正式学员要总学费/待缴学费/已缴学费设置为0(审核后才算付款)
        formalStudent.setAlreadyPaid(BigDecimal.ZERO);
        formalStudent.setRemainPaid(formalStudent.getTotalTuition());
        //入学时间即制表时间
        formalStudent.setEnrollTime(new Date());
        //插入formalStudent
        studentMapper.insert(formalStudent);
        income.getStudent().setId(formalStudent.getId());
        incomeMapper.insert(income);
    }

    @Override
    public void upgrade(Long nowClz, Long newClz) {
        studentMapper.upgrade(nowClz,newClz);
    }
}